package top.sqdpt.file.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.sqdpt.file.entity.dto.FileUpload;
import top.sqdpt.file.factory.FileStorageFactory;
import top.sqdpt.file.service.FileService;
import top.sqdpt.file.strategy.FileStorageStrategy;

import java.io.IOException;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileStorageFactory factory;

    @Override
    public String saveFile(FileUpload fileUpload) {
        FileStorageStrategy fileStorageStrategy = factory.getFileStorageStrategy();
        try {
            return fileStorageStrategy.saveFile(fileUpload);
        } catch (IOException e) {
            throw new RuntimeException("保存文件失败");
        }
    }

    @Override
    public String saveFile(String text) {
        FileStorageStrategy fileStorageStrategy = factory.getFileStorageStrategy();
        try {
            return fileStorageStrategy.saveFile(text);
        } catch (IOException e) {
            throw new RuntimeException("保存文件失败");
        }
    }
}
